package com.hishop.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hishop.common.utils.PageUtils;
import com.hishop.common.utils.Query;
import com.hishop.product.dao.AttrAttrgroupRelationDao;
import com.hishop.product.dao.AttrGroupDao;
import com.hishop.product.entity.AttrAttrgroupRelationEntity;
import com.hishop.product.entity.AttrEntity;
import com.hishop.product.entity.AttrGroupEntity;
import com.hishop.product.service.AttrAttrgroupRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {
    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    private AttrGroupDao attrGroupDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 通过属性id获取分组id
     * @param attrId
     * @return
     */
    @Override
    public Long selectGroupIdByAttrId(Long attrId) {
        return attrAttrgroupRelationDao.selectGroupIdByAttrId(attrId);
    }

    /**
     * 查询分组名称
     * @param attrId
     * @return
     */
    @Override
    public String selectGroupName(AttrEntity attr, Long attrId) {

        /**
         * 获取分组 id【有一些没有分组，所以为null， 要判断不为空在设置进去】
         * 获取分组对象拿到分组名字【分组 id为空，查全部，下面设置分组 id不为空在设置分组id、name就可以了】
         */
        Long attrGroupId = attrAttrgroupRelationDao.selectGroupIdByAttrId(attrId);
        AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrGroupId);

        /**
         * 设置分组id【有一些没有分组，所以为null， 要判断不为空在设置进去】
         * 设置分组名称
         */
        if (attrGroupId != null) {
            attr.setAttrGroupId(attrGroupId);
            attr.setGroupName(attrGroupEntity.getAttrGroupName());
        } else {
            attr.setGroupName("——");
        }
        return attr.getGroupName();
    }

    /**
     * 规格参数 修改数据同步更新 相关关联表
     *
     * @param attr
     */
    @Override
    public void updateGroupId(AttrEntity attr) {
        attrAttrgroupRelationDao.updateGroupId(attr);
        // System.out.println("4444444444444444444444" + attr.getAttrGroupId());
    }

    /**
     * 新建属性数据时，新增关联表数据
     *
     * @param attr
     */
    @Override
    public void saveDetail(AttrEntity attr) {
        // 获取 属性id
        Long attrId = attr.getAttrId();

        // 设置 属性和分组关联数据
        AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
        relationEntity.setAttrId(attrId);
        relationEntity.setAttrGroupId(attr.getAttrGroupId());
        attrAttrgroupRelationDao.insert(relationEntity);
    }

    /**
     * 根据分组id查出属性id
     *
     * @param attrGroupId 分组id
     * @return
     */
    @Override
    public Long[] selectAttrId(Long attrGroupId) {
        return attrAttrgroupRelationDao.selectAttrId(attrGroupId);
    }

    /**
     * 根据属性Id获取 属性和分组实体
     *
     * @param attrId
     * @return
     */
    @Override
    public AttrAttrgroupRelationEntity selectByAttrId(Long attrId) {
        return attrAttrgroupRelationDao.selectByAttrId(attrId);
    }


}